﻿using System;
using System.Data;
using System.Data.Common;

namespace Avin.Infrastructure.DbContext
{
    public interface IDbContextBase
    {
        /// <summary>
        /// 数据库类型
        /// </summary>
        DbProviderType DbProviderType { get; }

        /// <summary>
        /// 数据库提供器工厂
        /// </summary>
        DbProviderFactory DbProviderFactory { get; }

        /// <summary>
        /// 创建一个DbCommand
        /// </summary>
        /// <returns></returns>
        DbCommand CreateDbCommand();

        /// <summary>
        /// 创建一个DbConnection
        /// </summary>
        /// <returns></returns>
        DbConnection CreateDbConnection();

        /// <summary>
        /// 创建一个DbParameter
        /// </summary>
        /// <returns></returns>
        DbParameter CreateDbParameter();

        /// <summary>
        /// 创建一个DbParameter数组
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        DbParameter[] CreateDbParameters(int count = 1);

        /// <summary>
        /// 更改数据库链接以及数据库类型
        /// </summary>
        /// <param name="connectionString">链接字符串</param>
        /// <param name="providerType">数据库类型</param>
        void ChangeDataBase(string connectionString, DbProviderType providerType);

        /// <summary>
        /// 设置CommandTimeout
        /// </summary>
        /// <param name="commandTimeout"></param>
        void SetCommandTimeout(int commandTimeout);

        /// <summary>
        /// 插入用户语句到事务
        /// </summary>
        /// <param name="sqlType">1：增加，2：编辑，3：删除</param>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <param name="source"></param>
        void InsertUserSQL(int sqlType, string sql, DbParameter[] parameters = null, object source = null);

        /// <summary>
        /// 清除事务用户语句
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        void ClearUserSQL();

        /// <summary>
        /// 保存所有修改
        /// </summary>
        /// <returns></returns>
        int SaveChanges();

        /// <summary>
        /// 输出异常日志
        /// </summary>
        /// <param name="ex"></param>
        void LogError(string sql, Exception ex);

        /// <summary>
        /// 释放资源
        /// </summary>
        void Dispose();

        /// <summary>
        /// 查询sql，返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(string sql);

        /// <summary>
        /// 查询sql，返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(string sql, DbParameter[] parameters);

        /// <summary>
        /// 查询sql，返回DataSet
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(string sql, DbParameter[] parameters, CommandType commandType);

        /// <summary>
        /// 查询sql，返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        int ExecuteNonQuery(string sql);

        /// <summary>
        /// 查询sql，返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        int ExecuteNonQuery(string sql, DbParameter[] parameters);

        /// <summary>
        /// 查询sql，返回受影响行数
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        int ExecuteNonQuery(string sql, DbParameter[] parameters, CommandType commandType);

        /// <summary>
        /// 查询sql，返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        DbDataReader ExecuteReader(string sql);

        /// <summary>
        /// 查询sql，返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        DbDataReader ExecuteReader(string sql, DbParameter[] parameters);

        /// <summary>
        /// 查询sql，返回第一行第一列
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        DbDataReader ExecuteReader(string sql, DbParameter[] parameters, CommandType commandType);

        /// <summary>
        /// 查询sql，返回DataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        object ExecuteScalar(string sql);

        /// <summary>
        /// 查询sql，返回DataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        object ExecuteScalar(string sql, DbParameter[] parameters);

        /// <summary>
        /// 查询sql，返回DataReader
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="parameters"></param>
        /// <param name="commandType"></param>
        /// <returns></returns>
        object ExecuteScalar(string sql, DbParameter[] parameters, CommandType commandType);
    }
}